<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Event extends Site {

    function setup() {
        $this->run_modes('invite_list', 'submit_application', 'invite_inpage', 'invite_page', 'invite_result', 'detail', 'show', 'ajax', 'emailApplication', 'clear');
        $this->start_mode('invite_list');
        $this->selfurl = '/event.php';
        $this->pagesize = 5;
        parent::setup();
        include("common.php");
    }

    function cgiapp_prerun($rm = '') {
        $this->Product = getModel('Model_Product');
        $this->Event_application = getModel('Model_EventApplication');
        $this->Lookup = getModel('Model_Lookup');
        $this->Event = getModel('Model_Event');
        $this->member = $this->s_param('memberData');
        $this->ModelMember = getModel('Model_Member');
        $this->Points = getModel('Model_Points');
        $this->email_obj = getLibClass('itrialEmail');
        parent::cgiapp_prerun();
    }

    function action_invite_list() {
        $page = $this->get_q('page', 1, 'int');
        $s = $this->get_q('s', 0, 'int'); //下拉所選項
        $type = $this->get_q('type', 0, 'int'); //活動類型所選項

        $orderby = 'TB1.starttime desc';
        if ($s == 2 || $s == 3) {
            $orderby = 'TB1.event_date desc';
        }

        $kvcobj = getCache('event_invite_list', array('expire' => KVC_EXPIRE));

        $event_type_list = $kvcobj->get('event_type_list');
        if (!$event_type_list) {
            $event_type_list = $this->Lookup->getLookup('event');
            $kvcobj->set('event_type_list', $event_type_list);
        }

        $event_list = $kvcobj->get($page . '_' . $s . '_' . $type . '_event_list');
        if (!$event_list) {
            $event_list = $this->Event->getEventList($type, $s, $orderby, $page, $this->pagesize);
            $kvcobj->set($page . '_' . $s . '_' . $type . '_event_list', $event_list);
        }
        $event_list_res = $this->Event->get_list_res($event_list['result'], $s);

        $pagelist = showPage($event_list['pageinfo']['total'], $this->pagesize, $page, optimize_url('events', 'invite_list', 'type=' . $type . '&s=' . $s));
        $select = array(0 => '全部活動', 1 => '活動邀請', 2 => '過往活動', 3 => '活動報導');

        $this->tmpl_assign('select', $select);
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('event_list_res', $event_list_res);
        $this->tmpl_assign('s', $s);
        $this->tmpl_assign('type', $type);
        $this->tmpl_assign('event_type_list', $event_type_list);
        return $this->load_tmpl('e_invite_list.php');
    }

    function action_invite_inpage() {

        $eid = $this->get_q('eid', 1, 'int');
        $page = $this->get_q('page', 1, 'int');
        $kvcobj = getCache('event_invite_inpage', array('expire' => KVC_EXPIRE));
        $kvcobj1 = getCache('eventbyid', array('expire' => KVC_EXPIRE));
        $event = $kvcobj1->get($eid);
        if (!$event) {
            $event = $this->Event->getEventById($eid);
            $times = str_diff_time($event['endtime']);
            if (empty($event)) {
                return showMsgCode('活動不存在！', '/');
            } elseif (empty($times)) {
                return showMsgCode('活動已結束！', optimize_url('events', 'invite_result', 'eid=' . $event['id']));
            }

            $kvcobj1->set($eid, $event);
        }

//        if (time() < strtotime($event['starttime'])) {
//            return showMsgCode('活動時間有誤！', '/');
//        }

        $event['is_begin'] = time() < strtotime($event['starttime']) ? 0 : 1;

        //人氣自增1
        db_handle('update event set click=click+1 where id=' . mysql_escape_string($eid));


        $event['_difTime'] = explode(" ", str_diff_time($event['endtime']));
        $userlist = $kvcobj->get($eid . '_userlist');
        if (!$userlist) {
            $alluser = $this->Event_application->getAllUser($eid, 2);
            $userlist = isset($alluser['result']) && !empty($alluser['result']) ? $alluser['result'] : array();
            if (!empty($userlist)) {
                foreach ($userlist as $k => $v) {
                    $userlist[$k]['photo_l'] = imore_avatar($v['photo']);
                }
            }
            $kvcobj->set($eid . '_userlist', $userlist);
        }

        $event_application_list = $kvcobj->get($eid . '_' . $page . '_event_application_list');
        if (!$event_application_list) {
            $event_application_list = $this->Event_application->getApplicationList($eid, 4, "TB1.id desc", $page, $this->pagesize);
            $kvcobj->set($eid . '_' . $page . '_event_application_list', $event_application_list);
        }
        $reportlist = isset($event_application_list['result']) && !empty($event_application_list['result']) ? $event_application_list['result'] : array();
        if ($reportlist) {
            foreach ($reportlist as $k => $v) {
                $reportlist[$k]['photo_l'] = imore_avatar($v['photo']);
            }
        }

        $pagelist = showPage($event_application_list['pageinfo']['total'], $this->pagesize, $page, optimize_url('events', 'invite_inpage', '&eid=' . $eid), 1000);
        $event['_photo_url'] = !empty($event['has_detail']) ? PhotoResize($event['cover2'], 158, 158) : PhotoResize($event['cover'], 158, 158);


        //是否隱藏已申請會員及三部曲部份
        $turnpage = $page > 1 ? 1 : 0;

        if (!empty($this->member['id'])) {
            $_mem = $this->ModelMember->getMemberCondition($this->member['id']);
            $_member_arr = array();
            if ($_mem['result'])
                $_member_arr = $_mem['result'][0];
            if (!empty($_member_arr['address']) && !empty($_member_arr['homepage'])) {
                $s = 1;
            } else {
                $s = 0;
            }
        } else {
            $s = 0;
        }
        $this->tmpl_assign('s', $s);
        $this->tmpl_assign('eid', $eid);
        $this->tmpl_assign('turnpage', $turnpage);
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('event', $event);
        $this->tmpl_assign('userlist', $userlist);
        $this->tmpl_assign('reportlist', $reportlist);
        $this->tmpl_assign('NO_USERNAME', NO_USERNAME);
        $this->tmpl_assign('NO_PASSWORD', NO_PASSWORD);
        $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);

        $global_url = $this->param('URL');
        $this->tmpl_assign('global_url', $global_url);
        $this->tmpl_assign('thisUrl', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        $pageValue['title'] = $event['title'] . " | ";
        $this->tmpl_assign('pageValue', $pageValue);
        return $this->load_tmpl('e_invite_inpage.php');
    }

    function action_invite_page() {
        $eid = intval($this->get_q('eid', 0, 'int'));
        $m = $this->get_q('m', '');
        $md5_eid = md5($eid . 'imoreHK');
        if ($m != $md5_eid) {
            return showMsgCode('鏈接有誤！', '/');
        }
        $page = $this->get_q('page', 1, 'int');
		$kvcobj = getCache('event_invite_page', array('expire' => KVC_EXPIRE));
        $kvcobj1 = getCache('eventbyid', array('expire' => KVC_EXPIRE));
        $event = $kvcobj1->get($eid . "_pre");
        if (!$event) {
            $event = $this->Event->getEventById($eid, 2);
            $times = str_diff_time($event['endtime']);
            if (empty($event)) {
                return showMsgCode('活動不存在！', '/');
            } elseif (empty($times)) {
                return showMsgCode('活動已結束！', optimize_url('events', 'invite_result', 'eid=' . $event['id']));
            }

            $kvcobj1->set($eid . "_pre", $event);
        }

//        if (time() < strtotime($event['starttime'])) {
//            return showMsgCode('活動時間有誤！', '/');
//        }

        $event['is_begin'] = time() < strtotime($event['starttime']) ? 0 : 1;

        //人氣自增1
        db_handle('update event set click=click+1 where id=' . mysql_escape_string($eid));

        $event['_difTime'] = explode(" ", str_diff_time($event['endtime']));
        $userlist = $kvcobj->get($eid . '_userlist');
        if (!$userlist) {
            $alluser = $this->Event_application->getAllUser($eid, 2);
            $userlist = isset($alluser['result']) && !empty($alluser['result']) ? $alluser['result'] : array();
            if (!empty($userlist)) {
                foreach ($userlist as $k => $v) {
                    $userlist[$k]['photo_l'] = imore_avatar($v['photo']);
                }
            }
            $kvcobj->set($eid . '_userlist', $userlist);
        }

        $event_application_list = $kvcobj->get($eid . '_' . $page . '_event_application_list');
        if (!$event_application_list) {
            $event_application_list = $this->Event_application->getApplicationList($eid, 4, "TB1.id desc", $page, $this->pagesize);
            $kvcobj->set($eid . '_' . $page . '_event_application_list', $event_application_list);
        }
        $reportlist = isset($event_application_list['result']) && !empty($event_application_list['result']) ? $event_application_list['result'] : array();
        if ($reportlist) {
            foreach ($reportlist as $k => $v) {
                $reportlist[$k]['photo_l'] = imore_avatar($v['photo']);
            }
        }

        $pagelist = showPage($event_application_list['pageinfo']['total'], $this->pagesize, $page, optimize_url('events', 'invite_inpage', '&eid=' . $eid), 1000);
        $event['_photo_url'] = !empty($event['has_detail']) ? PhotoResize($event['cover2'], 158, 158) : PhotoResize($event['cover'], 158, 158);


        //是否隱藏已申請會員及三部曲部份
        $turnpage = $page > 1 ? 1 : 0;

        if (!empty($this->member['id'])) {
            $_mem = $this->ModelMember->getMemberCondition($this->member['id']);
            $_member_arr = array();
            if ($_mem['result'])
                $_member_arr = $_mem['result'][0];
            if (!empty($_member_arr['address']) && !empty($_member_arr['homepage'])) {
                $s = 1;
            } else {
                $s = 0;
            }
        } else {
            $s = 0;
        }

        $this->tmpl_assign('s', $s);
        $this->tmpl_assign('eid', $eid);
        $this->tmpl_assign('turnpage', $turnpage);
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('event', $event);
        $this->tmpl_assign('userlist', $userlist);
        $this->tmpl_assign('reportlist', $reportlist);
        $this->tmpl_assign('NO_USERNAME', NO_USERNAME);
        $this->tmpl_assign('NO_PASSWORD', NO_PASSWORD);
        $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);

        $global_url = $this->param('URL');
        $this->tmpl_assign('global_url', $global_url);
        $this->tmpl_assign('thisUrl', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        $pageValue['title'] = $event['title'] . " | ";
        $this->tmpl_assign('pageValue', $pageValue);
        return $this->load_tmpl('e_invite_inpage.php');
    }

    function action_invite_result() {
        $eid = $this->get_q('eid', 1, 'int');
        $page = $this->get_q('page', 1, 'int');
        $kvcobj = getCache('event_invite_result', array('expire' => KVC_EXPIRE));
        $kvcobj1 = getCache('eventbyid', array('expire' => KVC_EXPIRE));
        $event = $kvcobj1->get($eid);
        if (!$event) {
            $event = $this->Event->getEventById($eid);
            if (empty($event)) {
                return showMsgCode('活動不存在！', '/');
            }
            $kvcobj1->set($eid, $event);
        }
//        if (time() < strtotime($event['endtime'])) {
//            return showMsgCode('活動時間有誤！', '/');
//        }
        //人氣自增1
        db_handle('update event set click=click+1 where id=' . mysql_escape_string($eid));

        $event['_difTime'] = str_diff_time($event['endtime']);

        $userlist1 = $kvcobj->get($eid . '_userlist1');
        if (!$userlist1) {
            $alluser1 = $this->Event_application->getAllUser($eid, 2);
            $userlist1 = isset($alluser1['result']) && !empty($alluser1['result']) ? $alluser1['result'] : array();
            if (!empty($userlist1)) {
                foreach ($userlist1 as $k => $v) {
                    $userlist1[$k]['photo_l'] = imore_avatar($v['photo']);
                }
            }
            $kvcobj->set($eid . '_userlist1', $userlist1);
        }

        $userlist2 = $kvcobj->get($eid . '_userlist2');
        if (!$userlist2) {
            $alluser2 = $this->Event_application->getAllUser($eid, 0);
            $userlist2 = isset($alluser2['result']) && !empty($alluser2['result']) ? $alluser2['result'] : array();
            if (!empty($userlist2)) {
                foreach ($userlist2 as $k => $v) {
                    $userlist2[$k]['photo_l'] = imore_avatar($v['photo']);
                }
            }
            $kvcobj->set($eid . '_userlist2', $userlist2);
        }


        $event_application_list = $kvcobj->get($eid . '_' . $page . '_event_application_list');
        if (!$event_application_list) {
            $event_application_list = $this->Event_application->getApplicationList($eid, 4, "TB1.id desc", $page, $this->pagesize);
            $kvcobj->set($eid . '_' . $page . '_event_application_list', $event_application_list);
        }
        $reportlist = isset($event_application_list['result']) && !empty($event_application_list['result']) ? $event_application_list['result'] : array();
        if ($reportlist) {
            foreach ($reportlist as $k => $v) {
                $reportlist[$k]['photo_l'] = imore_avatar($v['photo']);
            }
        }

        $pagelist = showPage($event_application_list['pageinfo']['total'], $this->pagesize, $page, optimize_url('events', 'invite_inpage', '&eid=' . $eid), 1000);
        $event['_photo_url'] = !empty($event['has_detail']) ? PhotoResize($event['cover2'], 158, 158) : PhotoResize($event['cover'], 158, 158);

        //echo '<pre>'.var_export($event,1).'</pre>';
        //是否隱藏已申請會員及三部曲部份
        $turnpage = $page > 1 ? 1 : 0;

        $this->tmpl_assign('eid', $eid);
        $this->tmpl_assign('turnpage', $turnpage);
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('event', $event);
        $this->tmpl_assign('userlist1', $userlist1);
        $this->tmpl_assign('userlist2', $userlist2);
        $this->tmpl_assign('reportlist', $reportlist);
        $pageValue['title'] = $event['title'] . " | ";
        $this->tmpl_assign('pageValue', $pageValue);
        return $this->load_tmpl('e_invite_result.php');
    }

    function action_submit_application() {
        $eid = $this->get_q('eid', 1, 'int');

        if (!$this->member) {
            $toURL = optimize_url('events', 'submit_application', 'eid=' . $eid);
            $this->s_param('toURL', $toURL);
            return showMsgCode(HAS_LOGIN, '/member.php?action=login');
        }

        $c = db_query_count('event_application', "mid=" . mysql_escape_string($this->member['id']) . " and oid=" . mysql_escape_string($eid));
        if ($c > 0) {
            return showMsgCode('您已申請該活動', '/');
        }
        $kvcobj1 = getCache('eventbyid', array('expire' => KVC_EXPIRE));
        $event = $kvcobj1->get($eid);
        if (!$event) {
            $event = $this->Event->getEventById($eid);
            if (empty($event)) {
                return showMsgCode('活動不存在！', '/');
            }
            $kvcobj1->set($eid, $event);
        }

        if ($this->get_q('issubmit') == 'true') {
            if (empty($this->member)) {
                $this->iframeReturn(null, 'callback', array('retcode' => 9, 'msg' => NO_LOGIN));
            }
            $_c = db_query_count('event_application', "oid=" . mysql_escape_string($eid) . " and mid=" . mysql_escape_string($this->member['id']));
            if ($_c > 0) {
                $this->iframeReturn(null, 'callback', array('retcode' => 9, 'msg' => HAS_APPLY));
            }

            $attr = $mjson = array();
            $attr['oid'] = $eid;
            $attr['mid'] = $this->member['id'];
            $attr['application_status'] = 1;
            $attr['status'] = 1;
            $attr['body'] = htmlspecialchars($this->get_q('reason', ''));
            /**
              $reason_size = intval($this->get_q('reason_size', 0, 'int'));
              if (empty($attr['body']) || intval($reason_size) > 100) {
              $this->iframeReturn(null, 'callback', array('retcode' => 1, 'msg' => HAS_INVITE_REASON));
              }

              $mjson['itrial_url'] = htmlspecialchars($this->get_q('itrial_url',''));
              if(empty($mjson['itrial_url'])){
              $this->iframeReturn(null,'callback',array('retcode'=>2,'msg'=>HAS_REPORT_URL));
              }

              $mjson['www_url2'] = htmlspecialchars($this->get_q('www_url2',''));
              if(empty($mjson['www_url2'])){
              $this->iframeReturn(null,'callback',array('retcode'=>3,'msg'=>HAS_WWW_URL));
              }
             * */
            $mjson['photo'] = $this->get_q('photo_path', '');
            if (empty($mjson['photo'])) {
                $this->iframeReturn(null, 'callback', array('retcode' => 4, 'msg' => HAS_INVITE_PIC));
            }

            $ques = $this->get_q('ques');

            if ($ques) {
                foreach ($ques as $k => $v) {
                    if (empty($v['a'])) {
                        $this->iframeReturn(array('k' => $k), 'callback', array('retcode' => 5, 'msg' => HAS_REPORT_QUESTION));
                    }
                    if (is_array($mjson['questions'][$k]['a'])) {
                        $mjson['questions'][$k]['a'] = implode(',', $v['a']);
                    } else {
                        $mjson['questions'][$k] = htmlspecialchars($v['a']);
                    }
                }
            }

            $isread = $this->get_q('isread', 0, 'int');
            if (empty($isread)) {
                $this->iframeReturn(null, 'callback', array('retcode' => 7, 'msg' => HAS_AGREE_TERMS));
            }

            if ($this->get_q('facebook')) {
                $attr['is_fb_share'] = 1;
            }

            $attr['message_json'] = json_encode($mjson);
            $_invites = $this->Points->points_record($attr['mid'], "submit_event_invite", str_replace("[活動名稱]", $event['title'], SUBMIT_EVENT_INVITE_LANG), $eid, $event['points']);
            if (intval($_invites['ret']) == 0) {
                $this->iframeReturn(null, 'callback', array('retcode' => 9, 'msg' => '您的積分不足，無法申請活動 - ' . $event['title']));
            }
            $res = db_add('event_application', $attr);
            if ($res) {
                //$_data = db_query_one('event', array('field' => 'apply_num', 'where' => 'id=' . $attr['oid']));
                $apply_num = intval($event['apply_num']) + 1;
                db_update('event', array('apply_num' => $apply_num), ' id=' . mysql_escape_string($attr['oid']));
                $global_url = $this->param('URL');
                $emailUrl = $global_url . optimize_url('events', 'invite_inpage', 'eid=' . $attr['oid']);

                $this->email_obj->send($attr['mid'], "app_try_fc", array('cname' => $event['title'], 'emailUrl' => $emailUrl));

                $this->iframeReturn(null, 'callback', array('retcode' => 10, 'msg' => INVITE_SUCCESS));
            }
        } else {
            if (time() < strtotime($event['starttime'])) {
                return showMsgCode('活動時間有誤！', '/');
            }
            $event['_photo_url'] = !empty($event['has_detail']) ? PhotoResize($event['cover2'], 158, 158) : PhotoResize($event['cover'], 158, 158);
            $event['_difTime'] = explode(" ", str_diff_time($event['endtime']));
            $event['intro'] = wordscut($event['intro'], 50);
            $event['questions'] = json_decode($event['questions_json'], 1);
            $this->tmpl_assign('agree', HAS_AGREE_TERMS);
            $this->tmpl_assign('eid', $eid);
            $this->tmpl_assign('event', $event);
            $pageValue['title'] = $event['title'] . " | ";
            $this->tmpl_assign('pageValue', $pageValue);
            return $this->load_tmpl('e_submit_application.php');
        }
    }

    function action_detail() {
        $eid = $this->get_q('eid', 1, 'int');
        $kvcobj1 = getCache('eventbyid', array('expire' => KVC_EXPIRE));
        $event = $kvcobj1->get($eid);
        if (!$event) {
            $event = $this->Event->getEventById($eid);
            if (empty($event)) {
                return showMsgCode('活動不存在！', '/');
            }
            $kvcobj1->set($eid, $event);
        }
        if (empty($event['has_detail'])) {
            return showMsgCode('活動暫無報導！', '/');
        }
        $userlist = $this->Event_application->getAllUser($eid);
        if ($userlist['result']) {
            foreach ($userlist['result'] as $k => $v) {
                $userlist['result'][$k]['photo_l'] = imore_avatar($v['photo']);
            }
        }
        $products = explode(',', $event['product']);
        $pic_list = json_decode($event['content_json'], 1);

        $product_list = $_temp = array();
        if ($products) {
            foreach ($products as $k => $v) {
                $_temp[$k] = $this->Product->getProductById($v);
            }
        }

        if ($_temp) {
            foreach ($_temp as $k => $v) {
                if (!empty($v)) {
                    $product_list[$k]['id'] = $v['id'];
                    $product_list[$k]['name'] = $v['name'];
                    $product_list[$k]['cname'] = $v['cname'];
                    $product_list[$k]['member_type'] = $v['member_type'];
                    $product_list[$k]['cover'] = PhotoResize($v['cover'], 88, 88);
                }
            }
        }

        if (is_array($pic_list)) {
            foreach ($pic_list as $k => $v) {
                $pic_list[$k]['p1'] = PhotoResize($v['p'], 144, 101);
                $pic_list[$k]['p2'] = PhotoResize($v['p'], 649, 452);
            }
        }

        $event['_video'] = empty($event['video']) ? '' : youtube($event['video'], array('w' => 640, 'h' => 390));

        $tmpl = 'e_detail.php';
        if (!empty($event['event_user'])) {
            $tmpl = 'e_detail_n.php';
            foreach ($event['event_user'] as $key => $value) {
                $event['event_user'][$key]['photo_l'] = PhotoResize($value['photo_image1'], 90, 90);
                if (!empty($value['event_user_blog'])) {
                    foreach ($value['event_user_blog'] as $key1 => $value1) {
                        $event['event_user'][$key]['event_user_blog'][$key1]['img1'] = PhotoResize($value1['photo1_image1'], 144, 101);
                        $event['event_user'][$key]['event_user_blog'][$key1]['img2'] = PhotoResize($value1['photo2_image1'], 144, 101);
                        $event['event_user'][$key]['event_user_blog'][$key1]['img3'] = PhotoResize($value1['photo3_image1'], 144, 101);
                    }
                }
            }
        }

        $event_array = array();
        if ($event['id'] == 3) {
            $event_array = include("./veet_data/event_3/event_3.php");
//            if ($member_array) {
//                foreach ($member_array as $key => $value) {
//                    $member_ret = $this->ModelMember->getMember($value['id']);
//                    if ($member_ret['result']) {
//                        $member_ret['result'][0]['url'] = $value['url'];
//                        $member_ret['result'][0]['photo_l'] = imore_avatar($member_ret['result'][0]['photo']);
//                        $member_array[] = $member_ret['result'][0];
//                    }
//                    $member_array[$key]['photo_l'] = PhotoResize($value['photo'], 56, 56);
//                }
//            }
            if (!empty($event_array) && is_array($event_array) && !empty($event_array['member'])){
                $tmpl = "e_detail_veet.php";
            }
        }


        $this->tmpl_assign('eid', $eid);
        $this->tmpl_assign('event', $event);
        $this->tmpl_assign('userlist', $userlist);
        $this->tmpl_assign('product_list', $product_list);
        $this->tmpl_assign('pic_list', $pic_list);
        $this->tmpl_assign('event_array', $event_array);
        $pageValue = array();
        $pageValue['title'] = $event['title'] . " | ";
        $this->tmpl_assign('pageValue', $pageValue);
        //echo '<pre>'.var_export($event,1).'</pre>';
        return $this->load_tmpl($tmpl);
    }

    function action_show() {
        $eid = intval($this->get_q('eid', 1, 'int'));
        $aid = intval($this->get_q('aid', 1, 'int'));
        $issub = intval($this->get_q('or', 0));

        $memberData = $this->s_param('memberData');
        if (!$memberData) {
            $toURL = optimize_url('events', 'show', 'eid=' . $eid . "&aid=" . $aid . "&issubmit=" . $issub);
            $this->s_param('toURL', $toURL);
            return showMsgCode(HAS_LOGIN, '/member.php?action=login');
        }

        $c = db_query_count('event_application', "id=" . mysql_escape_string($aid) . " and (application_status=9 or application_status=11)");
        if ($c > 0) {
            return showMsgCode('活動已確認或拒絕', '/');
        }


        $kvcobj1 = getCache('eventbyid', array('expire' => KVC_EXPIRE));
        $event = $kvcobj1->get($eid);
        if (!$event) {
            $event = $this->Event->getEventById($eid);
            if (empty($event)) {
                return showMsgCode('活動不存在！', '/');
            }
            $kvcobj1->set($eid, $event);
        }
        if (time() < strtotime($event['starttime'])) {
            return showMsgCode('活動時間有誤！', '/');
        }

        $event_application = $this->Event_application->getApplicationList(0, 0, "", 1, 1, $aid);
        if ($event_application['result']) {
            $result = $event_application['result'][0];
            if ($result['application_status'] <> 7) {
                return showMsgCode('審核狀態不正確', '/');
            } else {
                if (!empty($issub)) {
                    $or = intval($this->get_q('or', 9, 'int'));
                    $db_updata = db_update('event_application', array('application_status' => $or), 'id=' . mysql_escape_string($aid));
                    $msg = $or == 9 ? base64_encode(base64_encode('你已接受活動邀請‘' . $event['title'] . '’！')) : base64_encode(base64_encode('你已拒絕邀請‘' . $event['title'] . '’！'));

                    if ($db_updata) {
                        if ($or == 9) {
                            $this->Event->update_i_agree($event, $this->member['id']);
                            $this->Points->points_record($this->member['id'], "confirm_event_invite", str_replace("[活動名稱]", $event['title'], CONFIRM_EVENT_INVITE_LANG), $eid, $event['points']);
                        } elseif ($or == 11) {
                            $this->Points->points_record($this->member['id'], "unfreeze_event_invite", str_replace("[活動名稱]", $event['title'], UNFREEZE_EVENT_INVITE_LANG), $eid, $event['points']);
                        }
                    }
                    //header('Location:/member_center.php?action=invite&msg=' . $msg);
                    $this->redirect('/member_center.php?action=invite&msg=' . $msg);
                } else {
                    $event['_photo_url'] = !empty($event['has_detail']) ? PhotoResize($event['cover2'], 180, 180) : PhotoResize($event['cover'], 180, 180);

                    $bt_url = array();
                    $bt_url[1] = optimize_url('events', 'show', 'issubmit=1&eid=' . $eid . '&aid=' . $aid . '&or=9');
                    $bt_url[2] = optimize_url('events', 'show', 'issubmit=1&eid=' . $eid . '&aid=' . $aid . '&or=11');

                    $this->tmpl_assign('bt_url', $bt_url);
                    $this->tmpl_assign('event', $event);
                    $pageValue['title'] = $event['name'] . " - " . $event['cname'] . " - ";
                    $this->tmpl_assign('pageValue', $pageValue);
                    //echo '<pre>'.var_export($event,1).'</pre>';
                    return $this->load_tmpl('e_show.php');
                }
            }
        } else {
            return showMsgCode('id出錯', '/');
        }
    }

    function action_ajax() {
        $t = $this->get_q('t', 1, 'int');
        switch ($t) {
            case 1:
                $ret = array();
                if (!empty($this->member)) {
                    $ret['login'] = 1;

                    $_mem1 = $this->ModelMember->getMember($this->member['id']);
                    if ($_mem1['result'][0]['email_status'] != 1) {
                        $ret['email'] = 0;
                        echo json_encode($ret);
                        exit();
                    } else {
                        $ret['email'] = 1;
                    }

                    $_mem = $this->ModelMember->getMemberCondition($this->member['id']);
                    $_member_arr = array();
                    if ($_mem['result'])
                        $_member_arr = $_mem['result'][0];

                    $eid = intval($this->get_q('eid', 0, 'int'));
                    $_pdata = db_query_one('event', array('field' => 'member_type,starttime,points,title', 'where' => 'id=' . $eid));


                    if (empty($_member_arr['address'])) {
                        $ret['limit'] = 0;
                        $ret['msg'] = LIMIT_INVITE;
                        echo json_encode($ret);
                        exit();
                    } elseif ((empty($_member_arr['homepage'])) && intval($_pdata['result']['member_type']) == 5) {
                        $ret['limit'] = 0;
                        $ret['msg'] = LIMIT_INVITE_EXPERIENCE;
                        echo json_encode($ret);
                        exit();
                    } else {
                        $ret['limit'] = 1;
                    }

                    $_data = db_query_count('event_application', "oid=" . mysql_escape_string($eid) . " and mid=" . mysql_escape_string($this->member['id']));
                    if ($_data > 0) {
                        $ret['invite'] = 0;
                        $ret['msg'] = HAS_APPLY;
                        echo json_encode($ret);
                        exit();
                    } else {
                        $ret['invite'] = 1;
                    }

                    if (intval($_mem1['result'][0]['points']) < intval($_pdata['result']['points'])) {
                        $ret['invite'] = 0;
                        $ret['msg'] = '您的積分不足，無法申請活動 - ' . $_pdata['result']['title'];
                        echo json_encode($ret);
                        exit();
                    }
                } else {
                    $ret['login'] = 0;
                    $ret['msg'] = NO_LOGIN;
                }
                echo json_encode($ret);
                exit();
                break;
            default:
                echo 'default error';
                break;
        }
    }

    function action_emailApplication() {

        $aid = intval($this->get_q('aid', 1, 'int'));
        $key = $this->get_q('key');

        if ($key <> md5($aid . "imoreHK"))
            exit("key error");

        $event_application = $this->Event_application->getApplicationList(0, 0, "", 1, 1, $aid);

        if ($event_application['result']) {
            $result = $event_application['result'][0];
            if ($result['application_status'] <> 7) {
                return showMsgCode('審核狀態不正確', '/');
            } else {
                $global_url = $this->param('URL');
                $eid = $result['oid'];
                $kvcobj1 = getCache('eventbyid', array('expire' => KVC_EXPIRE));
                $event = $kvcobj1->get($eid);
                if (!$event) {
                    $event = $this->Event->getEventById($eid);
                    if (empty($event)) {
                        return showMsgCode('活動不存在！', '/');
                    }
                    $kvcobj1->set($eid, $event);
                }
                $event['_photo_url'] = !empty($event['has_detail']) ? PhotoResize($event['cover2'], 180, 180) : PhotoResize($event['cover'], 180, 180);

                $bt_url = $global_url . optimize_url('events', 'show', 'eid=' . $eid . '&aid=' . $aid);

                $eventarr = array();
                $event_type_arr = $this->Lookup->getLookup('event');
                if ($event_type_arr['result']) {
                    foreach ($event_type_arr['result'] as $value) {
                        $eventarr[$value['id']] = $value['name'];
                    }
                }

                $event_type = $eventarr[$event['event_type']];

                $h_param = array(
                    'username' => $result['username'],
                    'event_type' => $event_type,
                    'title' => $event['title'],
                    'accept_endtime' => $result['accept_endtime'],
                    'bt_url' => $bt_url,
                );

                $this->email_obj->send($result['mid'], "invited_to_try_e", $h_param);

                echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />email發送成功";
            }
        } else {
            return showMsgCode('id出錯', '/');
        }
    }

    function action_clear() {
        $kvc = trim($this->get_q('kvc', ''));
        clear_kvcache($kvc);
    }

}

$webapp = new Event();
$webapp->run();
?>